package kr.hkit.powerjava.chapter22.work;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class TestMain {
	public static void main(String[] args) {
		ArrayList<Department> deptList = new ArrayList<>();
		DepartmetnDAO deptDao = new DepartmentDAO(deptList);
		
		deptDao.addItem(new Department(1, "컴공", 4));
		
		
		
		ArrayList<Student> stdList = new ArrayList<>();
		initList(stdList);
		StudentManager sm = new StudentManager(stdList);
		Student std = null;
		
		int res = 0;
		do{
			try{
				res = Integer.parseInt(JOptionPane.showInputDialog("1.추가, 2.검색, 3.수정, 4.삭제, 5.리스트, 6.종료"));
			}catch(NumberFormatException e){
				res = 6;
			}
			switch(res){
			case 1:
				std = inputStdInfo(true);
				sm.addStudent(std);
				break;
			case 2:
				std = sm.searchStd(inputStdInfo(false));
				if(std == null){
					System.out.println("해당 학생 없음!");
				}else{
					System.out.println(std);				
				}break;
			case 3:
				std = inputStdInfo(true);
				sm.reStudent(std);
				break;
			case 4:
				std = inputStdInfo(false);
				sm.delStudent(std);
				break;
			case 5:
				sm.prnList();
				break;
			default:
				break;
			}
		}while(res < 6);
		
		/*ArrayList<Student> stdList = new ArrayList<>();
		initList(stdList);
		prnHeader();
		prnList(stdList);
		
		
		sm.prnList();
		
		Student std = new Student(stdList.size()+1, "제시카", 90, 45, 55);
		sm.addStudent(std);
		sm.prnList();
		
		sm.delStudent(std);
		sm.prnList();
		
		
		Student std = new Student(2, "이민정");
		System.out.println(stdList.lastIndexOf(std) + "의 위치에 존재 " + stdList.contains(std));
	//	lastIndexOf() 끝에서부터 검색, indexOf() 앞에서부터 검색
		
		총점순 정렬
		Collections.sort(stdList);
		prnHeader();
		prnList(stdList);
		수학점수별 정렬
		Comparator<Student> comp = new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getMathScore()-o2.getMathScore();
			}
		};
		Collections.sort(stdList, comp);
		prnHeader();
		prnList(stdList);*/
	}

	private static Student inputStdInfo(boolean gubun) {
		if(gubun){
			String std = JOptionPane.showInputDialog("학생번호 성명 국어 영어 수학");
			StringTokenizer st = new StringTokenizer(std);
			int stdNum = Integer.parseInt(st.nextToken());
			String stdName = st.nextToken();
			int kor = Integer.parseInt(st.nextToken());
			int eng = Integer.parseInt(st.nextToken());
			int math = Integer.parseInt(st.nextToken());
			return new Student(stdNum, stdName, kor, eng, math);
		}else{
			String std = JOptionPane.showInputDialog("학생번호 성명");
			StringTokenizer st = new StringTokenizer(std);
			int stdNum = Integer.parseInt(st.nextToken());
			String stdName = st.nextToken();
			return new Student(stdNum, stdName);
		}
	}

	private static void prnHeader() {
		String header = String.format("%3s\t %5s\t %4s\t %4s\t %4s\t %4s\t %5.2s","학번", "성명", "국어", "영어", "수학", "총점", "평균");
		System.out.println(header);
	}

	private static void prnList(ArrayList<Student> stdList) {
		for(Student std : stdList){
			System.out.println(std);
		}
	}

	private static void initList(ArrayList<Student> stdList) {
		stdList.add(new Student(1, "김태희", 87, 87, 87));
		stdList.add(new Student(2, "이민정", 90, 87, 87));
		stdList.add(new Student(3, "이소은", 70, 70, 87));
		stdList.add(new Student(4, "남주호", 60, 65, 65));
		stdList.add(new Student(5, "신한욱", 50, 55, 50));
		stdList.add(new Student(6, "이민정", 90, 87, 100));
	}
	
}
